//
//  ModelViewModel.swift
//  Automobile
//
//  Created by erkebulan elzhan on 8/2/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import Foundation


class ModalViewModel {
    
    var modalData:[ModalView.ModalType: [ModalView.ModalData]] = [
        .color: [],
        .privod: [
            ModalView.ModalData(name: "Передний"),
            ModalView.ModalData(name: "Задний"),
            ModalView.ModalData(name: "Полный")
        ],
        .korobka: [],
        .dvigatel: [],
        .kuzov: [],
        .rule: [],
        .state: []
    ]
    
    var pickerData:[ModalView.ModalType:[Double]] = [
        .power: [10, 20, 30, 40, 50, 60, 70, 80],
        .year: [2011,2012,2013,2014,2015,2016,2017,2018],
        .probeg: [],
        .expenditures: [],
        .volume: [],
        .price: [0,1,2,3],
        .firstPurchase: [],
        .monthPurchase: []
    ]
    
    
    init () {
        pickerData[.probeg] = self.getNumbers(from: 1000, to: 10000, interval: 1000)
        pickerData[.expenditures] = self.getNumbers(from: 1, to: 20, interval: 1)
        pickerData[.volume] = self.getNumbers(from: 0.5, to: 4, interval: 0.5)
    }
    
    public func loadData (modal: ModalView.ModalType, completion: @escaping (RequestError?) -> Void) {

        let pathData: [ModalView.ModalType : [String: Any]] = [
            .korobka: [
                "path": "filters/transmission-types/",
                "base": Request.Service.advert
            ],
            .color: [
                "path": "garage/colors/",
                "base": Request.Service.garage
            ],
            .privod: [
                "path": "filters/drive-types/",
                "base": Request.Service.advert
            ],
            .kuzov: [
                "path": "filters/body-types/",
                "base": Request.Service.advert
            ],
            .dvigatel: [
                "path": "filters/engine-types/",
                "base": Request.Service.advert
            ],
            .rule: [
                "path": "filters/rule-types/",
                "base": Request.Service.advert
            ],
            .state: [
                "path": "filters/condition-types/",
                "base": Request.Service.advert
            ]
        ]
        
        let backendService = Backend()
        let _request = Request(base: pathData[modal]!["base"] as! Request.Service)
        let request = URLRequest(url: _request.getUrl(path: pathData[modal]!["path"] as! String))
                
        if modal == .rule || modal == .state {
            backendService.fetchFromBackend(for: [ModalView.ModalData].self, request: request, statusCode: 200) { (result) in
                switch result {
                    case .failure(let error):
                        completion(error)
                    case .success(let response):
                        self.modalData[modal] = response
                        completion(nil)
                }
            }
        } else {
            backendService.fetchFromBackend(for: Response<[ModalView.ModalData]>.self, request: request, statusCode: 200) { (result) in
                switch result {
                    case .failure(let error):
                        completion(error)
                    case .success(let response):
                        self.modalData[modal] = response.results
                        completion(nil)
                }
            }
        }
    }

    func resetModal (type: ModalView.ModalType) {
        if modalData.keys.contains(type) {
            for (i, _) in modalData[type]!.enumerated() {
                modalData[type]![i].isSelected? = false
            }
        }
    }
    
    func getNumbers (from: Double, to: Double, interval: Double) -> [Double] {
        var output: [Double] = []
        for _current in stride(from: from, to: to, by: interval) {
            output.append(_current)
        }
        return output
    }
}
